package com.guli.mall.pms.util;
/*
 * @Author 罗
 * @date 2020/9/12 - 9:07 下午
 */

import com.guli.common.utils.QuickSort;
import com.guli.mall.pms.entity.CategoryEntity;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class ListToTree2 {

    public static void parse(List<CategoryEntity> list, CategoryEntity categoryEntity, Long parentCid, Comparator<CategoryEntity> comparator) {

        CategoryEntity temp = null;

        for (int i = 0; i < list.size(); i++) {

            temp = list.get(i);

            if (parentCid.equals(temp.getParentCid())) {

                if (ObjectUtils.isEmpty(categoryEntity.getChildren())) {
                    categoryEntity.setChildren(new ArrayList<CategoryEntity>());
                }

                categoryEntity.getChildren().add(temp);

                list.remove(i);

                i--;

                parse(list, temp, temp.getCatId(), comparator);
            }
        }

        if (!ObjectUtils.isEmpty(categoryEntity.getChildren())) {

            QuickSort.sort(categoryEntity.getChildren(), 0, categoryEntity.getChildren().size() - 1, comparator);
        }


    }

}
